﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BrokerBase.Lib.Tool
{
    public static class BaseTools
    {
        public static void CopyTo<T>(T source, T target) where T : class
        {

            try
            {

                PropertyInfo[] sps = source.GetType().GetProperties().OrderBy(p => p.Name).ToArray<PropertyInfo>();
                PropertyInfo[] tps = target.GetType().GetProperties().OrderBy(p => p.Name).ToArray<PropertyInfo>();

                for (int i = 0; i < sps.Count(); i++)
                {
                    try
                    {

                        PropertyInfo sp = sps[i];

                        if (sp.CanWrite)
                        {
                            tps[i].SetValue(target, sp.GetValue(source, null), null);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("对象copy时发生错误" + ex.Message);
            }

        }
    }
}
